package com.qf.springmvc.controller;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class MyDoubleSerializer extends JsonSerializer<Double> {
    @Override
    public void serialize(Double value, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        System.out.println("输入是数据是:" + value);  // 11.2483D
        // BigDecimal 数学包的一个类 设置精度 四舍五入
        String number = BigDecimal.valueOf(value).setScale(2, RoundingMode.HALF_UP).toString();
        System.out.println("四舍五入的结果是是:" + number);  // 11.25D
        jsonGenerator.writeNumber(number);
    }
}